<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>移动端适配之postcss-px-to-viewport | 20秒的勇气</title>
    <meta name="generator" content="VuePress 1.5.3">
    <script>var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?01611e9c2d4c65531fe791d0a83e6ebd";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })()</script>
    <meta name="description" content="欢迎来到wenbintian的博客">
    <link rel="preload" href="/blog/assets/css/0.styles.5dc057b1.css" as="style"><link rel="preload" href="/blog/assets/js/app.2a047d56.js" as="script"><link rel="preload" href="/blog/assets/js/2.cc145974.js" as="script"><link rel="preload" href="/blog/assets/js/37.a8305f70.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.dc9f6a74.js"><link rel="prefetch" href="/blog/assets/js/11.f7ecaf96.js"><link rel="prefetch" href="/blog/assets/js/12.4363725a.js"><link rel="prefetch" href="/blog/assets/js/13.0fd4b251.js"><link rel="prefetch" href="/blog/assets/js/14.edc7bd5f.js"><link rel="prefetch" href="/blog/assets/js/15.647c0964.js"><link rel="prefetch" href="/blog/assets/js/16.2e8857c5.js"><link rel="prefetch" href="/blog/assets/js/17.1c7cc9ee.js"><link rel="prefetch" href="/blog/assets/js/18.bf88a22d.js"><link rel="prefetch" href="/blog/assets/js/19.1433c5a8.js"><link rel="prefetch" href="/blog/assets/js/20.713720d3.js"><link rel="prefetch" href="/blog/assets/js/21.51a3d47b.js"><link rel="prefetch" href="/blog/assets/js/22.04488762.js"><link rel="prefetch" href="/blog/assets/js/23.b9337410.js"><link rel="prefetch" href="/blog/assets/js/24.d0d5b562.js"><link rel="prefetch" href="/blog/assets/js/25.e76a54ef.js"><link rel="prefetch" href="/blog/assets/js/26.006d0afa.js"><link rel="prefetch" href="/blog/assets/js/27.38ec3468.js"><link rel="prefetch" href="/blog/assets/js/28.c8cf7077.js"><link rel="prefetch" href="/blog/assets/js/29.70f03487.js"><link rel="prefetch" href="/blog/assets/js/3.9bfe5560.js"><link rel="prefetch" href="/blog/assets/js/30.3e6d6ba9.js"><link rel="prefetch" href="/blog/assets/js/31.1dbd85cb.js"><link rel="prefetch" href="/blog/assets/js/32.208e4272.js"><link rel="prefetch" href="/blog/assets/js/33.979228dd.js"><link rel="prefetch" href="/blog/assets/js/34.6ad6fae2.js"><link rel="prefetch" href="/blog/assets/js/35.59b3e591.js"><link rel="prefetch" href="/blog/assets/js/36.7239d917.js"><link rel="prefetch" href="/blog/assets/js/38.7e3197cd.js"><link rel="prefetch" href="/blog/assets/js/39.1bb3ad3b.js"><link rel="prefetch" href="/blog/assets/js/4.3b6a7e49.js"><link rel="prefetch" href="/blog/assets/js/40.bc263e99.js"><link rel="prefetch" href="/blog/assets/js/41.4ee25f6a.js"><link rel="prefetch" href="/blog/assets/js/42.37c6cc26.js"><link rel="prefetch" href="/blog/assets/js/43.9a40d780.js"><link rel="prefetch" href="/blog/assets/js/44.2eda0bb6.js"><link rel="prefetch" href="/blog/assets/js/45.ce3372d9.js"><link rel="prefetch" href="/blog/assets/js/46.17e44796.js"><link rel="prefetch" href="/blog/assets/js/47.f845ed6d.js"><link rel="prefetch" href="/blog/assets/js/5.bc6e1aa4.js"><link rel="prefetch" href="/blog/assets/js/6.e0f80af7.js"><link rel="prefetch" href="/blog/assets/js/7.c808e2d2.js"><link rel="prefetch" href="/blog/assets/js/8.902b76dc.js"><link rel="prefetch" href="/blog/assets/js/9.9bb8a7fe.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.5dc057b1.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><!----> <span class="site-name">20秒的勇气</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/knowledge/" class="nav-link router-link-active">
  知识篇
</a></div><div class="nav-item"><a href="/blog/notes/" class="nav-link">
  随身笔记
</a></div> <a href="https://github.com/wenbintian" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/blog/knowledge/" class="nav-link router-link-active">
  知识篇
</a></div><div class="nav-item"><a href="/blog/notes/" class="nav-link">
  随身笔记
</a></div> <a href="https://github.com/wenbintian" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><a href="/blog/knowledge/book/" class="sidebar-heading clickable"><span>读书篇</span> <span class="arrow right"></span></a> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><a href="/blog/knowledge/tool/" class="sidebar-heading clickable router-link-active open"><span>工具篇</span> <span class="arrow down"></span></a> <ul class="sidebar-links sidebar-group-items"><li><a href="/blog/knowledge/tool/tool1.html" class="sidebar-link">git 操作指南</a></li><li><a href="/blog/knowledge/tool/tool2.html" aria-current="page" class="active sidebar-link">移动端适配之postcss-px-to-viewport</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/knowledge/tool/tool2.html#安装" class="sidebar-link">安装</a></li><li class="sidebar-sub-header"><a href="/blog/knowledge/tool/tool2.html#默认配置" class="sidebar-link">默认配置</a></li><li class="sidebar-sub-header"><a href="/blog/knowledge/tool/tool2.html#项目中如何配置" class="sidebar-link">项目中如何配置</a></li><li class="sidebar-sub-header"><a href="/blog/knowledge/tool/tool2.html#忽略某些行的转换" class="sidebar-link">忽略某些行的转换</a></li></ul></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><a href="/blog/knowledge/css/" class="sidebar-heading clickable"><span>CSS篇</span> <span class="arrow right"></span></a> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><a href="/blog/knowledge/js/" class="sidebar-heading clickable"><span>JS篇</span> <span class="arrow right"></span></a> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="移动端适配之postcss-px-to-viewport"><a href="#移动端适配之postcss-px-to-viewport" class="header-anchor">#</a> 移动端适配之postcss-px-to-viewport</h1> <h2 id="安装"><a href="#安装" class="header-anchor">#</a> 安装</h2> <ul><li>使用npm安装</li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">npm</span> <span class="token function">install</span> postcss-px-to-viewport --save-dev
</code></pre></div><ul><li>或者使用yarn安装</li></ul> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">yarn</span> <span class="token function">add</span> -D postcss-px-to-viewport
</code></pre></div><h2 id="默认配置"><a href="#默认配置" class="header-anchor">#</a> 默认配置</h2> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">{</span>
    unitToConvert<span class="token operator">:</span> <span class="token string">'px'</span><span class="token punctuation">,</span>
    viewportWidth<span class="token operator">:</span> <span class="token number">320</span><span class="token punctuation">,</span>
    unitPrecision<span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span>
    propList<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'*'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    viewportUnit<span class="token operator">:</span> <span class="token string">'vw'</span><span class="token punctuation">,</span>
    fontViewportUnit<span class="token operator">:</span> <span class="token string">'vw'</span><span class="token punctuation">,</span>
    selectorBlackList<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    minPixelValue<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
    mediaQuery<span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    replace<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
    exclude<span class="token operator">:</span> <span class="token keyword">undefined</span><span class="token punctuation">,</span>
    include<span class="token operator">:</span> <span class="token keyword">undefined</span><span class="token punctuation">,</span>
    landscape<span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    landscapeUnit<span class="token operator">:</span> <span class="token string">'vw'</span><span class="token punctuation">,</span>
    landscapeWidth<span class="token operator">:</span> <span class="token number">568</span>
<span class="token punctuation">}</span>
</code></pre></div><ul><li><code>unitToConvert (String)</code> 需要转换的单位，默认为&quot;px&quot;</li> <li><code>viewportWidth (Number)</code> 设计稿的视口宽度</li> <li><code>unitPrecision (Number)</code> 单位转换后保留的精度</li> <li><code>propList (Array)</code> 能转化为vw的属性列表
<ul><li>传入特定的CSS属性；</li> <li>可以传入通配符<code>&quot;*&quot;</code>去匹配所有属性，例如：<code>[&quot;&quot;]</code>；</li> <li>在属性的前或后添加<code>&quot;*&quot;</code>,可以匹配特定的属性. (例如<code>['*position*']</code>会匹配<code>background-position-y</code>)</li> <li>在特定属性前加<code>&quot;!&quot;</code>，将不转换该属性的单位. 例如:<code>['*', '!letter-spacing']</code>，将不转换<code>letter-spacing</code></li> <li><code>&quot;!&quot; 和 &quot;*&quot;</code>可以组合使用， 例如: <code>['*', '!font*']</code>，将不转换<code>font-size</code>以及<code>font-weight</code>等属性</li></ul></li> <li><code>viewportUnit (String)</code> 希望使用的视口单位</li> <li><code>fontViewportUnit (String)</code> 字体使用的视口单位</li> <li><code>selectorBlackList (Array)</code> 需要忽略的CSS选择器，不会转为视口单位，使用原有的px等单位。
<ul><li>如果传入的值为字符串的话，只要选择器中含有传入值就会被匹配
<ul><li>例如<code>selectorBlackList</code>为 ['body'] 的话， 那么<code>.body-class</code>就会被忽略</li></ul></li> <li>如果传入的值为正则表达式的话，那么就会依据CSS选择器是否匹配该正则
<ul><li>例如<code>selectorBlackList</code>为 [/^body$/] , 那么 body 会被忽略，而<code>.body</code>不会</li></ul></li></ul></li> <li><code>minPixelValue (Number)</code> 设置最小的转换数值，如果为1的话，只有大于1的值会被转换</li> <li><code>mediaQuery (Boolean)</code> 媒体查询里的单位是否需要转换单位</li> <li><code>replace (Boolean)</code> 是否直接更换属性值，而不添加备用属性</li> <li><code>exclude (Array or Regexp)</code> 忽略某些文件夹下的文件或特定文件，例如<code>'node_modules'</code>下的文件
<ul><li>如果值是一个正则表达式，那么匹配这个正则的文件会被忽略</li> <li>如果传入的值是一个数组，那么数组里的值必须为正则</li></ul></li> <li><code>include (Array or Regexp)</code> 如果设置了<code>include</code>，那将只有匹配到的文件才会被转换，例如只转换<code>'src/mobile'</code>下的文件 <code>(include: /\/src\/mobile\//)</code> <ul><li>如果值是一个正则表达式，将包含匹配的文件，否则将排除该文件</li> <li>如果传入的值是一个数组，那么数组里的值必须为正则</li></ul></li> <li><code>landscape (Boolean)</code> 是否添加根据 landscapeWidth 生成的媒体查询条件 @media (orientation: landscape)</li> <li><code>landscapeUnit (String)</code> 横屏时使用的单位</li> <li><code>landscapeWidth (Number)</code> 横屏时使用的视口宽度</li></ul> <h2 id="项目中如何配置"><a href="#项目中如何配置" class="header-anchor">#</a> 项目中如何配置</h2> <ul><li>方法一.在 <code>package.json</code>文件下配置</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">// package.json</span>
<span class="token punctuation">{</span>
    <span class="token string">&quot;dependencies&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token string">&quot;postcss-px-to-viewport&quot;</span><span class="token operator">:</span> <span class="token string">&quot;^1.1.1&quot;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token string">&quot;postcss&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token string">&quot;plugins&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
            <span class="token string">&quot;autoprefixer&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token string">&quot;postcss-px-to-viewport&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                <span class="token string">&quot;viewportWidth&quot;</span><span class="token operator">:</span> <span class="token number">375</span><span class="token punctuation">,</span>
                <span class="token string">&quot;unitPrecision&quot;</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
                <span class="token string">&quot;minPixelValue&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token string">&quot;fontViewportUnit&quot;</span><span class="token operator">:</span> <span class="token string">&quot;vw&quot;</span><span class="token punctuation">,</span>
                <span class="token string">&quot;selectorBlackList&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token punctuation">]</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><ul><li>方法二.在根目录下新建文件 <code>postcss.config.js</code>文件</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">// postcss.config.js</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    plugins<span class="token operator">:</span> <span class="token punctuation">{</span>
        autoprefixer<span class="token operator">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token string">&quot;postcss-px-to-viewport&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token string">&quot;viewportWidth&quot;</span><span class="token operator">:</span> <span class="token number">375</span><span class="token punctuation">,</span>
        <span class="token string">&quot;unitPrecision&quot;</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
        <span class="token string">&quot;minPixelValue&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token string">&quot;fontViewportUnit&quot;</span><span class="token operator">:</span> <span class="token string">&quot;vw&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;selectorBlackList&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre></div><h2 id="忽略某些行的转换"><a href="#忽略某些行的转换" class="header-anchor">#</a> 忽略某些行的转换</h2> <p>可以使用特殊注释来忽略单行的转换</p> <ul><li><code>/* px-to-viewport-ignore-next */</code>    在某一行行上面，防止在下一行进行转换。</li> <li><code>/* px-to-viewport-ignore */</code>     在属性的后面，防止在当前行一行上进行转换。
<strong>example:</strong></li></ul> <div class="language-css extra-class"><pre class="language-css"><code><span class="token comment">/* example input: */</span>
<span class="token selector">.class</span> <span class="token punctuation">{</span>
  <span class="token comment">/* px-to-viewport-ignore-next */</span> // 阻止转换下一行
  <span class="token property">width</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
  <span class="token property">padding</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
  <span class="token property">height</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span> <span class="token comment">/* px-to-viewport-ignore */</span> // 阻止转换当前行
  <span class="token property">border</span><span class="token punctuation">:</span> solid 2px #000<span class="token punctuation">;</span> <span class="token comment">/* px-to-viewport-ignore */</span>
<span class="token punctuation">}</span>

<span class="token comment">/* example output: */</span>
<span class="token selector">.class</span> <span class="token punctuation">{</span>
  <span class="token property">width</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
  <span class="token property">padding</span><span class="token punctuation">:</span> 3.125vw<span class="token punctuation">;</span>
  <span class="token property">height</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
  <span class="token property">border</span><span class="token punctuation">:</span> solid 2px #000<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="参考"><a href="#参考" class="header-anchor">#</a> 参考</h4> <p><a href="https://github.com/evrone/postcss-px-to-viewport/blob/HEAD/README_CN.md" target="_blank" rel="noopener noreferrer">postcss-px-to-viewport<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">最后一次修改时间:</span> <span class="time">8/17/2020, 9:52:49 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/blog/knowledge/tool/tool1.html" class="prev">
        git 操作指南
      </a></span> <span class="next"><a href="/blog/knowledge/css/css1.html">
        CSS中line-height属性详解
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/blog/assets/js/app.2a047d56.js" defer></script><script src="/blog/assets/js/2.cc145974.js" defer></script><script src="/blog/assets/js/37.a8305f70.js" defer></script>
  </body>
</html>
